# e.g., loops, dictionaries
# e.g., strings are super easy to use like "arrays" or lists
x=[]
y=""
z=dict()
n=10
for i in range(n):
x += [i]
y += str(i)
z[i]=i
print(len(x), x, type(x))
print(len(y), y, type(y))
print(len(z), z, type(z))
10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>
10 0123456789 <class 'str'>
10 {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} <class 'dict'>
# e.g., functions, recurssion, conditionals
def fibonacci(sequence=[0,1], steps=6):
if steps==0:
return sequence
else:
return fibonacci(sequence+[sum(sequence[-2:])], steps=steps-1)
print(fibonacci())
[0, 1, 1, 2, 3, 5, 8, 13]
import numpy as np
n=1000000
%%timeit
_ = np.arange(n)*np.arange(n)
3.48 ms ± 243 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
_ = [i*j for i,j in zip(range(n),range(n))]
82.9 ms ± 2.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(np.zeros(10).reshape(5,2,1))
[[[0.] [0.]] [[0.] [0.]] [[0.] [0.]] [[0.] [0.]] [[0.] [0.]]]
print(np.ones([1,10]))
print(np.ones([10,1]))
np.ones([1,10])*np.ones([10,1])
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]] [[1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.] [1.]]
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
import scipy
from scipy import stats
print(scipy.integrate.quad(lambda x: stats.norm.pdf(x), -1, 1))
scipy.special.erf(1/np.sqrt(2)), 2*(stats.norm.cdf(1)-.5)
(0.682689492137086, 7.579375928402476e-15)
(0.6826894921370859, 0.6826894921370859)
scipy.special.erf?
n = 1000
print(stats.norm(loc=1, scale=2).rvs(n))
stats.norm.fit(stats.norm(loc=1, scale=2).rvs(n))
[ 5.02135051e+00 2.40032431e+00 2.30203951e+00 2.30921686e+00 2.21281026e+00 3.06057433e+00 4.49123612e-01 1.99547849e+00 3.23260238e-01 4.42212283e+00 3.67051850e+00 8.13393979e-02 -8.90453860e-01 3.37429503e+00 -5.48993555e+00 6.39233355e+00 6.07199121e-01 2.68417795e+00 -4.48919290e-01 -1.60186304e+00 1.39475167e+00 2.84441301e+00 -9.02703935e-02 1.21572897e+00 1.29009471e+00 9.34116633e-01 3.62024680e+00 3.44931150e+00 1.24799258e+00 -9.67033980e-02 -2.79635619e+00 5.78220677e-01 1.27616694e+00 2.00408948e+00 1.50321096e+00 -1.55220524e+00 -2.95467158e-01 4.08046637e+00 2.54929284e+00 2.99984135e+00 1.52729362e+00 -2.18543053e+00 -2.50246763e+00 2.81641032e-01 7.62264514e-01 4.89920110e+00 2.42746520e+00 3.13052446e+00 2.48973940e+00 2.95222505e+00 -4.24912966e-01 2.09976603e-01 1.78192472e+00 4.20545238e-01 -3.13420722e-02 -2.11991395e+00 7.60100342e-02 5.18083537e+00 3.40924443e+00 2.97133590e+00 1.15346131e+00 3.07879068e+00 2.23228630e+00 -5.67877002e-01 -2.18844221e+00 2.62021789e-02 9.02075425e-01 -6.16582738e-01 5.65996626e+00 5.69565768e-01 1.79704834e+00 3.60099299e+00 -1.22057252e+00 3.28989060e+00 2.28132425e-01 7.70193974e-01 3.91240952e+00 7.33653024e-01 2.79224326e+00 1.89590051e-01 -4.60371971e-01 9.13569717e-01 1.24100997e+00 2.50906295e+00 4.74657658e-01 1.65190910e+00 -1.03561442e+00 -8.12899069e-01 -3.81810141e+00 1.41968824e+00 9.06889328e-01 -1.28372660e+00 5.55850566e+00 5.55571349e-01 2.28860901e+00 6.95135430e+00 4.15166819e-01 3.13410147e+00 3.28941563e-01 1.16100021e+00 1.31238547e+00 3.85781979e+00 -3.47464361e-01 1.26838040e+00 -1.22972612e+00 7.65812353e-02 -1.58799732e+00 3.14454027e+00 -4.06492008e-01 1.70052679e+00 2.91087205e+00 1.72545755e+00 -4.83878810e-01 -8.46625743e-01 -1.69104871e-01 -1.08982039e-01 -8.21210407e-01 -3.89051056e+00 2.16333614e+00 -3.11757299e-01 4.19384471e+00 1.37878564e+00 -3.12167704e-01 7.29043016e-01 3.06748874e+00 -1.53556556e+00 2.25498263e+00 2.87663435e+00 2.71909122e+00 6.39419767e+00 1.14972251e+00 2.07491906e+00 1.48279685e+00 4.39711445e+00 1.15999215e+00 1.70890453e-01 2.14091039e+00 2.37456956e+00 4.00174748e+00 5.29449498e+00 4.80602791e-02 4.77941521e-02 -2.72611693e-01 -2.95010935e+00 -4.93487527e-01 1.32614794e+00 -7.10165409e-02 1.72223172e+00 -1.60488193e+00 4.29575367e-01 6.73605169e-01 3.01495468e-02 9.98606337e-01 1.22053582e+00 2.00020793e+00 1.37139431e-01 2.25635405e+00 -2.92507422e+00 2.19228127e+00 -4.50141257e-01 1.39214685e+00 -1.67917095e+00 2.15952984e+00 9.65359213e-02 -1.72706068e+00 3.26078602e+00 3.41152251e+00 3.34278310e+00 3.22317943e+00 3.76753541e+00 2.30889504e+00 1.33530628e-01 1.39266095e-01 3.81069251e-01 4.13403647e-02 2.16988023e+00 1.83113900e-01 2.13488778e+00 6.08875931e+00 2.59295344e+00 7.41736639e-01 1.00094484e+00 2.73905781e+00 3.28129292e+00 2.06527314e+00 -1.08751266e+00 1.84837763e+00 5.37728094e-02 -1.37210384e+00 2.77485429e+00 -1.28645295e+00 2.18076013e+00 2.17106400e+00 5.69036400e-01 -2.31765364e+00 1.35754461e-01 -3.24810484e-01 4.11358176e-01 1.53482803e+00 -2.06375845e+00 1.19407605e+00 1.90868630e+00 -1.60689418e+00 2.18687494e+00 1.42522428e+00 -3.51541544e+00 4.40903320e+00 -7.14372230e-02 1.39530044e+00 -1.85882008e+00 3.12905993e+00 1.20198122e+00 7.05579531e-01 5.36253856e-01 4.34264751e+00 -1.61231964e+00 -1.52751248e+00 3.73495130e+00 8.69595332e-01 2.85144007e+00 1.87524123e+00 2.74887322e+00 -1.12620882e+00 1.47694279e+00 -1.71970378e-01 1.20942975e+00 3.18859326e+00 -3.48933241e-01 2.49279123e+00 4.05902019e+00 1.32251715e+00 1.41501310e+00 2.27849788e+00 -5.23894537e-01 1.49414985e+00 9.33630256e-01 3.69884567e+00 -9.09700951e-01 7.49986729e-01 4.71157658e-02 1.58133188e+00 1.72173930e-01 7.15988366e-01 -7.64765974e-01 3.99516015e+00 -7.75689440e-01 4.01141714e-01 -2.39615369e+00 -1.58469712e+00 2.34068182e+00 -6.33711877e-01 1.39363600e+00 -7.58483074e-01 1.19477482e+00 3.37246357e+00 2.05929623e+00 -4.67597669e-01 -1.91722889e-01 -2.80244185e+00 -1.68459157e+00 1.87790985e+00 3.54390341e+00 2.74654536e+00 3.26699710e+00 -1.47180662e-01 5.31516377e+00 2.05147739e+00 -1.09045866e+00 3.22342910e+00 -3.04532178e+00 3.18041213e+00 3.22933961e+00 -8.55045489e-01 -4.96024127e-01 2.61899114e-01 1.69412922e+00 1.44982979e+00 -8.17786399e-01 5.34925298e-02 -1.12675163e+00 4.85939145e-01 1.89230456e+00 1.36085931e+00 2.98511546e-01 3.35521191e+00 2.95257814e+00 -2.49891159e+00 1.96431713e+00 -1.52893217e-02 3.38059279e+00 -1.65953738e+00 1.19733622e+00 -5.51680026e-01 3.72307066e+00 2.56541743e+00 -2.37398401e+00 -5.42226500e+00 -6.73846292e-01 4.47583603e+00 1.47641162e+00 -8.51780590e-01 8.49531647e-01 4.23072345e+00 -1.93351573e-03 3.70825364e+00 -4.39323166e+00 2.54684857e+00 1.34807387e+00 9.64698149e-02 -8.98978781e-01 -2.17504928e+00 1.25235793e+00 1.51724770e+00 2.83796269e+00 1.71767552e+00 1.48493543e+00 -1.97208879e+00 9.37364257e-01 2.19095833e+00 4.88794779e+00 4.97711844e+00 2.08937515e+00 3.00451387e+00 -7.87137846e-01 3.94788891e+00 3.32777112e+00 1.72234611e+00 4.97929235e+00 1.54220565e+00 -2.03790626e+00 -2.04753679e-01 5.72594626e-03 4.10829057e-01 2.71856096e-01 1.25961017e+00 2.60072157e-01 -7.83752782e-01 -6.51045665e-01 3.97765285e-01 -6.92927538e-01 3.69311796e+00 -7.43641633e-02 4.40558049e+00 2.23848184e+00 1.82106084e-01 3.81521925e+00 4.23385261e+00 4.37703886e-01 1.22804688e+00 1.07434868e+00 1.98063898e+00 2.04505560e+00 1.85116543e+00 6.01199804e-01 8.25909961e-01 -1.48816582e-01 1.23894392e+00 2.56267407e+00 -1.15743507e-02 -2.42436267e+00 2.03824793e+00 -3.92504841e+00 -2.26973861e+00 1.24921866e+00 1.74016734e+00 -1.09005667e-01 -6.27702130e-01 2.59591421e+00 -4.22668929e+00 3.97227526e+00 7.77972395e-01 3.56333540e+00 -1.93113008e+00 1.11554341e+00 4.25853910e-01 -1.44624218e+00 -2.08847984e-01 5.88163216e-01 3.26025701e+00 1.80820449e+00 1.77328919e+00 1.15071854e+00 -2.04241694e-01 1.18195005e+00 3.09609901e+00 7.45412145e-01 4.04947783e+00 3.08144778e-01 4.61939795e+00 3.53193718e+00 1.51960593e+00 5.52639112e+00 1.12878294e+00 -8.18209789e-01 3.23849330e-01 -1.96325469e+00 2.71782541e+00 -9.40249308e-01 5.12574419e-01 -1.81041721e-01 -8.31922394e-01 4.18299554e+00 3.57787362e+00 1.29324695e+00 4.42132432e+00 9.35334733e-01 1.07008218e+00 3.60953946e+00 3.50494964e+00 1.90021235e+00 6.28497155e-01 3.25915911e-01 4.07142062e+00 -1.82099710e+00 1.28718896e+00 -3.01120768e+00 -2.22706567e+00 5.09828600e+00 4.12091035e-01 5.31622433e-01 5.58166105e-01 -7.11325921e-02 -1.25281050e+00 -3.88229079e-01 1.76480444e+00 -6.93686353e-01 -3.58080279e-01 -7.68521713e-01 7.18696931e-01 2.92396669e+00 6.65243882e-01 2.15416328e+00 -1.49351521e+00 -2.71222292e+00 5.26473707e+00 9.47312906e-01 -4.81429007e-01 -2.62222727e+00 -1.04543527e+00 7.59241590e-01 1.37320560e+00 -4.42079322e-01 1.15947838e+00 4.28891988e+00 -1.73699618e+00 1.24819813e+00 1.73025843e+00 -1.61972311e+00 -8.51815954e-01 2.99645739e+00 -1.70227445e-01 2.59035284e+00 -1.25210124e+00 7.72498005e-01 2.31909855e+00 -1.30951678e+00 2.86353058e+00 -7.98724277e-01 -1.14534173e+00 4.72907260e+00 5.30810469e+00 2.55803374e-01 3.31677426e+00 -2.33219140e+00 1.99184969e+00 1.40112574e+00 2.47451287e+00 1.26557444e+00 2.57711421e+00 -5.94454824e-01 2.33765257e+00 4.15226339e+00 2.35092462e+00 9.81968858e-01 2.51598445e+00 -1.22812581e+00 -3.42829517e-01 4.64795092e+00 2.11691299e+00 -6.03232292e-03 -1.20832048e+00 2.55923062e+00 4.68567211e+00 1.32169237e+00 1.65364706e+00 1.78134081e+00 -4.32165120e-01 3.35589832e+00 -9.62589022e-01 -2.61963990e+00 -3.79361667e+00 2.16888935e+00 5.93512248e-02 2.01607382e+00 3.05636814e+00 5.70504112e+00 -2.56733882e+00 5.92538600e-02 -6.25880056e-02 1.84142015e-02 8.63406522e-01 7.55358808e-01 5.47577917e-01 2.80618573e+00 2.55087412e+00 2.30273701e+00 4.78627067e-01 -2.44308822e+00 1.17386767e+00 5.03439633e+00 -8.97117941e-01 2.76206360e+00 1.32024327e+00 8.57480679e-01 1.36451186e+00 3.76942682e-01 3.85489358e-01 7.15968417e-01 2.05937957e-01 1.30986277e+00 2.03239250e+00 2.05301628e+00 -3.61318806e-01 1.21165220e+00 -3.65511714e-02 2.29255163e+00 -4.40475421e-01 -2.16661886e-01 7.52200066e-02 -1.64359691e+00 1.95815768e+00 8.82806184e-01 -9.96088583e-01 -1.13234433e+00 2.31555982e+00 2.21504205e+00 3.16990695e+00 5.67175876e-01 5.37613769e-01 1.45492853e+00 -1.67778352e+00 -2.06962947e-01 7.59356230e-01 1.90154152e-01 9.26646527e-01 7.47380632e-02 2.48854260e+00 7.36854562e-01 2.66915982e+00 1.82598637e+00 5.89595316e-01 2.66803963e+00 3.56026059e-01 -1.28428540e+00 1.85074527e+00 -2.44953635e+00 -2.48191006e+00 1.54389457e+00 2.38479448e+00 3.27373175e+00 -7.89098210e-01 -4.96566677e-01 4.04184463e-01 -3.34365065e+00 -4.00443309e-01 8.89116431e-01 3.62800132e+00 4.81875841e+00 1.16841349e-01 -4.99343752e-01 2.11231133e+00 3.00132193e+00 1.28059669e+00 3.88012399e+00 3.18856383e-01 3.89978080e+00 -2.24832780e-01 -1.88635674e+00 -5.39165877e-01 3.53742027e+00 3.64591350e+00 -6.16283467e-01 5.92998524e+00 9.26339061e-01 3.71640817e+00 1.64546020e+00 9.24779202e-01 4.48226545e+00 -6.84518109e-01 5.46740401e+00 2.32971340e+00 1.28028388e+00 1.54587425e+00 -1.45617539e+00 8.40024202e-01 3.58296709e+00 -1.09276866e+00 5.22248318e-01 2.53336708e+00 3.91677508e+00 4.70451647e+00 2.43472205e+00 2.22271896e+00 5.45651824e+00 1.72984164e+00 1.04601218e+00 3.90610156e-01 -2.09359589e+00 -2.87525849e-01 2.04532634e+00 2.20827491e+00 2.32725600e-01 2.41521762e+00 4.38714853e+00 3.06699570e+00 3.05269743e+00 1.93787334e-01 7.29186975e-01 7.98813143e-01 1.06349458e+00 1.54857128e+00 4.77285370e-02 1.36544913e+00 -4.31461542e-01 1.58687621e+00 6.90233423e-02 5.15686935e+00 1.14728956e+00 1.31283319e-01 9.70377254e-01 2.26816649e+00 1.81646468e-02 2.11993763e+00 1.67083529e+00 -4.13298277e-01 2.17875277e+00 -3.38614215e-01 3.97268140e+00 3.59941454e+00 1.41228944e+00 2.96021921e+00 4.06627369e+00 2.94459669e+00 3.91086715e+00 4.38952894e+00 3.86996635e+00 1.46475647e+00 5.39218621e-02 -6.97543657e-02 2.12145156e+00 1.56650762e+00 7.01894475e+00 8.76376392e-01 -1.03585388e-01 5.56451177e-01 1.68359301e+00 2.10910613e+00 8.50006337e-01 -3.50127494e+00 3.41125724e+00 -7.52447751e-03 -6.51735845e-01 -2.29450614e-01 7.30799041e-01 1.41258912e+00 -3.60992293e+00 4.00573572e-01 6.15736237e+00 -1.06276325e+00 1.97710763e+00 9.37615256e-01 1.61605214e+00 1.66465510e+00 -2.16201703e+00 -1.25264200e+00 -1.32597047e+00 1.15325938e+00 1.58431547e+00 9.78992420e-01 4.21513638e+00 1.40179001e+00 6.31445183e-01 -3.59821409e-01 1.83679080e+00 4.78923098e+00 -6.20830883e-01 1.44618545e+00 2.40328480e+00 5.01628213e-01 7.37441226e-01 1.74153623e+00 5.63818778e-01 1.43096111e+00 3.09261881e+00 2.18485657e+00 2.59946202e-01 6.48975245e-01 -2.17855062e+00 1.54793218e+00 4.15321272e+00 2.64754194e+00 -1.51777026e+00 3.01488654e-01 2.15871250e+00 1.96384715e+00 -1.50725913e-01 -9.33104995e-01 -2.82023881e+00 -1.28909145e+00 5.62294505e-01 2.87714350e-01 1.50779828e+00 1.07924535e+00 1.59373874e+00 2.21867700e+00 -2.21978951e+00 -1.87299432e+00 -4.49973464e-01 2.43292671e+00 3.15064905e-01 3.40247849e+00 7.70288612e-01 -9.62470803e-01 4.28083697e+00 3.10862852e+00 4.61837942e-01 1.14415024e+00 2.96658169e+00 1.38028044e+00 7.14972290e+00 -2.40664063e+00 3.19615995e+00 -1.46387966e+00 4.16244300e+00 -1.56570831e+00 2.54225617e+00 1.08847773e+00 2.24078167e+00 2.97676403e+00 4.01366833e+00 -8.26938282e-01 2.91339165e+00 3.97827968e-01 8.96686821e-01 1.46155102e+00 1.63066451e+00 1.24130143e+00 2.80625476e+00 1.24064067e+00 -3.89838741e-01 2.91311149e+00 3.44129380e+00 -7.07473445e-01 -2.45218488e+00 2.11459745e-01 3.51378035e+00 -8.27793667e-01 5.72212469e+00 6.58122801e-02 1.28846071e+00 -6.60108097e-01 1.79497569e+00 -1.93276863e+00 1.53452436e+00 1.86024850e-01 4.56280000e+00 1.02697364e-01 -3.26733299e+00 1.68391865e+00 4.04694697e+00 2.60718890e+00 -1.36601031e+00 -6.03412491e-01 -1.56911144e+00 -1.80172998e+00 -1.30305311e+00 3.95662862e+00 1.43741940e+00 -7.85208223e-02 2.32463786e+00 1.35137423e+00 -5.22346110e-01 1.53809904e+00 2.15641175e+00 2.97245039e+00 8.49721456e-01 2.73741656e+00 -2.37095900e-01 2.86693363e+00 -7.27985243e-01 2.84008215e+00 -4.81080447e+00 -1.45846065e+00 1.16308986e+00 1.51385049e+00 2.77482369e+00 4.37514421e+00 7.28389023e-01 -1.41843266e+00 -1.96880542e+00 3.21999776e+00 1.17162458e+00 -1.57043695e+00 5.20870397e+00 2.85827402e+00 4.10370520e+00 1.87099505e-02 2.43799907e+00 2.62347210e+00 -1.87778073e+00 4.01028543e-01 2.04253807e+00 -1.47425376e+00 4.42807395e+00 -3.88962416e-01 1.23563363e+00 -7.61261147e-02 2.86536027e+00 3.12453122e+00 -1.92503349e-01 -1.32446962e-01 -2.54538408e+00 7.72744780e-01 -3.27845715e+00 -1.24223847e+00 1.12828593e+00 4.69298903e+00 2.53519847e+00 2.07862846e+00 2.38340138e+00 1.07683439e+00 -3.49695210e-01 -5.73551562e-01 4.21806609e+00 2.01086238e+00 1.68158263e+00 -2.38389984e+00 2.55380746e+00 -2.15321353e-01 3.52779835e+00 1.14351974e+00 4.57733904e+00 6.70772911e-01 -7.13142044e-01 2.07820415e+00 4.55755256e+00 1.70295138e+00 -4.60897405e+00 2.62963640e+00 -2.36384318e-01 -1.08321707e+00 -4.94012305e-01 2.52232037e+00 -6.82569797e-02 -1.92763969e+00 2.00176787e+00 1.03906052e+00 1.42135697e+00 -1.33099974e+00 1.71520779e+00 1.83802888e+00 1.26353456e+00 5.19358733e-01 -2.71991759e+00 -7.48832656e-02 -2.37373278e+00 3.95027776e+00 -2.65930699e-01 -1.02080011e+00 4.13978936e+00 1.47303433e+00 1.20200713e+00 2.86511073e+00 -1.14555807e+00 -1.63527529e+00 4.11195430e+00 -6.21731535e-01 1.79006615e+00 -2.17127619e+00 4.61236732e+00 2.81309598e+00 -1.36348590e+00 8.92353071e-01 -5.75463145e-02 2.71296058e+00 -5.68055864e-01 8.28318207e-01 5.33696652e-01 4.81460202e+00 1.02912783e+00 6.48335903e-01 1.88355821e+00 1.13909545e+00 -1.49217829e+00 2.96333749e+00 -1.01882231e-01 1.17781850e+00 1.09564320e+00 -2.04677373e+00 5.83601117e-01 1.72178419e+00 3.77122123e+00 1.60335713e+00 -1.34929223e-01 1.66608771e+00 3.09546803e+00 4.95595739e+00 5.60451841e-01 4.63243934e+00 1.20105853e+00 -1.78174319e+00 3.38122714e+00 9.42946379e-01 1.04106779e+00 1.38865450e+00 1.73360932e+00 9.88494684e-01 2.76643560e+00 1.46630011e+00 9.49019709e-02 1.93675755e+00 5.58273196e-01 2.65831056e+00 -1.15086449e+00 4.73103582e-01 1.68014507e-01 2.32104357e+00 8.07146328e-01 1.64629761e+00 2.88582013e+00 -1.62762025e+00 2.95669648e+00 -1.08629516e+00 2.68077784e+00 1.01351994e+00 9.96035598e-01 1.05960782e+00 -1.26912783e+00 8.28920405e-01 3.01755355e+00 1.58948582e+00 -2.03019801e+00 -2.28552327e+00 3.33785502e+00 4.94288554e-01 2.14424127e-01 -7.98600376e-01 -3.22468531e-01 2.44993399e+00 2.98100762e+00 9.69545715e-01 4.11243709e+00 1.81889262e-01 4.56586924e+00 3.78532945e+00 1.43999979e-01 9.37793233e-01 -9.17202476e-01 5.97038083e+00 4.92006436e-01 1.97978057e+00 -1.64312058e+00 1.81890281e+00 -2.61289256e-01 -1.67427508e+00 -1.63314129e+00 3.63923707e+00 -1.01275452e+00 9.58993241e-01 3.63122080e+00 2.31316899e+00 -1.54267143e+00 1.76717030e+00 1.38232353e+00 2.34393691e+00 1.83258648e+00 1.70355559e+00 -4.99240868e-01 1.62316010e+00 -5.07321587e-01 2.67011051e+00 2.86041728e+00 -1.41138801e-01 3.59701926e+00 4.41619176e+00 3.90586665e+00 -1.70452335e+00 4.44640690e-01 -9.75135388e-01 3.40534214e+00 3.41456357e+00 1.18599730e+00 -2.45107964e+00 3.09782085e+00 -9.09961243e-01 -5.20727780e-02 -3.64257768e+00 4.45388330e+00 -1.70372870e+00 1.87005864e+00]
(0.9833073950025236, 1.9340481476735356)
import pandas as pd
dataset = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv'
penguins = pd.read_csv(dataset, dtype={key:'category' for key in ['species','island','sex']})
penguins.head()
| species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | |
|---|---|---|---|---|---|---|---|
| 0 | Adelie | Torgersen | 39.1 | 18.7 | 181.0 | 3750.0 | MALE |
| 1 | Adelie | Torgersen | 39.5 | 17.4 | 186.0 | 3800.0 | FEMALE |
| 2 | Adelie | Torgersen | 40.3 | 18.0 | 195.0 | 3250.0 | FEMALE |
| 3 | Adelie | Torgersen | NaN | NaN | NaN | NaN | NaN |
| 4 | Adelie | Torgersen | 36.7 | 19.3 | 193.0 | 3450.0 | FEMALE |
penguins.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 344 entries, 0 to 343 Data columns (total 7 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 species 344 non-null category 1 island 344 non-null category 2 bill_length_mm 342 non-null float64 3 bill_depth_mm 342 non-null float64 4 flipper_length_mm 342 non-null float64 5 body_mass_g 342 non-null float64 6 sex 333 non-null category dtypes: category(3), float64(4) memory usage: 12.1 KB
# like the R `summrary()` function
penguins.describe()
| bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | |
|---|---|---|---|---|
| count | 342.000000 | 342.000000 | 342.000000 | 342.000000 |
| mean | 43.921930 | 17.151170 | 200.915205 | 4201.754386 |
| std | 5.459584 | 1.974793 | 14.061714 | 801.954536 |
| min | 32.100000 | 13.100000 | 172.000000 | 2700.000000 |
| 25% | 39.225000 | 15.600000 | 190.000000 | 3550.000000 |
| 50% | 44.450000 | 17.300000 | 197.000000 | 4050.000000 |
| 75% | 48.500000 | 18.700000 | 213.000000 | 4750.000000 |
| max | 59.600000 | 21.500000 | 231.000000 | 6300.000000 |
import matplotlib.pyplot as plt
tabs = []
fig = plt.figure(figsize=[8,4])
ax = fig.subplots(2,2).flatten()
i = 0
for col in penguins:
if penguins.dtypes[col]=='float64':
penguins[[col]].plot(kind='kde', ax=ax[i])
i+=1
elif penguins.dtypes[col]=='category':
tabs += [pd.DataFrame(penguins[col].value_counts(), columns=[col])]
else:
pass
plt.tight_layout()
plt.show()
_=[display(tab) for tab in tabs]
| species | |
|---|---|
| Adelie | 152 |
| Gentoo | 124 |
| Chinstrap | 68 |
| island | |
|---|---|
| Biscoe | 168 |
| Dream | 124 |
| Torgersen | 52 |
| sex | |
|---|---|
| MALE | 168 |
| FEMALE | 165 |
strikes_path = 'http://lib.stat.cmu.edu/datasets/strikes'
strikes = pd.read_csv(strikes_path, skiprows=18, header=None,
delim_whitespace=True)
strikes.columns = ['Country', 'Year', 'Stike_Volume', 'Unemplyment',
'Inflation', 'Socialist_Party_Representation',
'Unionization']
strikes[['Year']] = strikes[['Year']].astype(type(0))
strikes.head()
| Country | Year | Stike_Volume | Unemplyment | Inflation | Socialist_Party_Representation | Unionization | |
|---|---|---|---|---|---|---|---|
| 0 | 1.0 | 1951 | 296.0 | 1.3 | 19.8 | 43.0 | 0.375 |
| 1 | 1.0 | 1952 | 397.0 | 2.2 | 17.2 | 43.0 | 0.375 |
| 2 | 1.0 | 1953 | 360.0 | 2.5 | 4.3 | 43.0 | 0.375 |
| 3 | 1.0 | 1954 | 300.0 | 1.7 | 0.7 | 47.0 | 0.375 |
| 4 | 1.0 | 1955 | 326.0 | 1.4 | 2.0 | 38.5 | 0.375 |
SnP_path = 'http://lib.stat.cmu.edu/datasets/spdc2693'
SnP = pd.read_csv(SnP_path, skiprows=5, header=None,
delim_whitespace=True)
SnP.columns = ['Date', 'Close']
SnP['Date'] = SnP.Date.apply(lambda x: pd.to_datetime('19'+str(x), format='%Y%m%d'))
SnP['Year'] = SnP.Date.apply(lambda x: x.year)
SnP.head()
| Date | Close | Year | |
|---|---|---|---|
| 0 | 1926-01-08 | 12.78 | 1926 |
| 1 | 1926-01-15 | 12.52 | 1926 |
| 2 | 1926-01-22 | 12.45 | 1926 |
| 3 | 1926-01-29 | 12.74 | 1926 |
| 4 | 1926-02-05 | 12.87 | 1926 |
SnP_strikes = SnP.merge(strikes, on='Year', how='inner')
SnP_strikes.set_index('Date', inplace=True)
country_codes = {1.0: 'AusT',
2.0: 'AusA',
3.0: 'Bel',
4.0: 'Can',
5.0: 'Den',
6.0: 'Fin',
7.0: 'Fra',
8.0: 'Ger',
9.0: 'Ire',
10.0: 'Ita',
11.0: 'Jap',
12.0: 'Ned',
13.0: 'Nwz',
14.0: 'Nor',
15.0: 'Swe',
16.0: 'Swi',
17.0: 'UK',
18.0: 'Usa'}
SnP_strikes['Country'] = SnP_strikes.Country.map(country_codes)
strikes[['Country']] = pd.Categorical(strikes['Country'])
fig, ax = plt.subplots(2,1, figsize=(15,13))
#apply(lambda x: np.log(x) if x.name=='Stike_Volume' else x).\
SnP_strikes.groupby('Country').rolling(200).mean().reset_index(0).groupby('Country')['Unemplyment'].plot(ax=ax[0])
ax[0].set_title('Unemployment (%)')
ax[0].legend(loc='upper center')
SnP_strikes['Close'].plot(ax=ax[1])
ax[1].set_title('S&P 500 Closing Values')
Text(0.5, 1.0, 'S&P 500 Closing Values')
SnP_strikes[SnP_strikes.Country.apply(lambda x: x in ['Usa','UK'])].\
groupby(by=['Year','Country']).max()
| Close | Stike_Volume | Unemplyment | Inflation | Socialist_Party_Representation | Unionization | ||
|---|---|---|---|---|---|---|---|
| Year | Country | ||||||
| 1951 | UK | 23.85 | 81.0 | 0.9 | 9.7 | 47.2 | 0.375 |
| Usa | 23.85 | 466.0 | 3.2 | 8.0 | 51.0 | 0.000 | |
| 1952 | UK | 26.59 | 86.0 | 1.4 | 6.1 | 47.2 | 0.375 |
| Usa | 26.59 | 1190.0 | 2.9 | 2.2 | 51.0 | 0.000 | |
| 1953 | UK | 26.66 | 104.0 | 1.3 | 1.9 | 47.2 | 0.375 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 1983 | Usa | 172.65 | 192.0 | 9.5 | 3.2 | 46.0 | 0.000 |
| 1984 | UK | 170.41 | 1278.0 | 11.2 | 5.0 | 32.2 | 0.375 |
| Usa | 170.41 | 89.0 | 7.4 | 4.3 | 46.0 | 0.000 | |
| 1985 | UK | 212.02 | 298.0 | 11.5 | 6.1 | 32.2 | 0.375 |
| Usa | 212.02 | 73.0 | 7.1 | 3.6 | 47.0 | 0.000 |
70 rows × 6 columns
import seaborn as sns
SnP_strikes_US_UK = SnP_strikes[SnP_strikes.Country.apply(lambda x: x in ['Usa','UK'])].\
groupby(by=['Year','Country']).max().reset_index(1).dropna()
#SnP_strikes_US_UK.Country.cat.remove_unused_categories(inplace=True)
p = sns.pairplot(SnP_strikes_US_UK.iloc[:,:-1], hue="Country")
p.map_lower(sns.kdeplot, levels=4, color=".2")
p.map_upper(sns.histplot)
<seaborn.axisgrid.PairGrid at 0x7fbdb93f99a0>
sklearnpandas+statsmodels (not shown)#https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpc_iris.html#sphx-glr-auto-examples-gaussian-process-plot-gpc-iris-py
from sklearn import datasets
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = np.array(iris.target, dtype=int)
h = .02 # step size in the mesh
kernel = 1.0 * RBF([1.0])
gpc_rbf_isotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y)
kernel = 1.0 * RBF([1.0, 1.0])
gpc_rbf_anisotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y)
# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
titles = ["Isotropic RBF", "Anisotropic RBF"]
plt.figure(figsize=(10, 5))
for i, clf in enumerate((gpc_rbf_isotropic, gpc_rbf_anisotropic)):
# Plot the predicted probabilities. For that, we will assign a color to
# each point in the mesh [x_min, m_max]x[y_min, y_max].
plt.subplot(1, 2, i + 1)
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape((xx.shape[0], xx.shape[1], 3))
plt.imshow(Z, extent=(x_min, x_max, y_min, y_max), origin="lower")
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=np.array(["r", "g", "b"])[y],
edgecolors=(0, 0, 0))
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title("%s, LML: %.3f" %
(titles[i], clf.log_marginal_likelihood(clf.kernel_.theta)))
plt.tight_layout()
#https://scikit-learn.org/stable/auto_examples/ensemble/plot_gradient_boosting_regularization.html#sphx-glr-auto-examples-ensemble-plot-gradient-boosting-regularization-py
from sklearn import ensemble
X, y = datasets.make_hastie_10_2(n_samples=12000, random_state=1)
X = X.astype(np.float32)
# map labels from {-1, 1} to {0, 1}
labels, y = np.unique(y, return_inverse=True)
X_train, X_test = X[:2000], X[2000:]
y_train, y_test = y[:2000], y[2000:]
original_params = {'n_estimators': 1000, 'max_leaf_nodes': 4, 'max_depth': None, 'random_state': 2,
'min_samples_split': 5}
plt.figure()
for label, color, setting in [('No shrinkage', 'orange',
{'learning_rate': 1.0, 'subsample': 1.0}),
('learning_rate=0.1', 'turquoise',
{'learning_rate': 0.1, 'subsample': 1.0}),
('subsample=0.5', 'blue',
{'learning_rate': 1.0, 'subsample': 0.5}),
('learning_rate=0.1, subsample=0.5', 'gray',
{'learning_rate': 0.1, 'subsample': 0.5}),
('learning_rate=0.1, max_features=2', 'magenta',
{'learning_rate': 0.1, 'max_features': 2})]:
params = dict(original_params)
params.update(setting)
clf = ensemble.GradientBoostingClassifier(**params)
clf.fit(X_train, y_train)
# compute test set deviance
test_deviance = np.zeros((params['n_estimators'],), dtype=np.float64)
for i, y_pred in enumerate(clf.staged_decision_function(X_test)):
# clf.loss_ assumes that y_test[i] in {0, 1}
test_deviance[i] = clf.loss_(y_test, y_pred)
plt.plot((np.arange(test_deviance.shape[0]) + 1)[::5], test_deviance[::5],
'-', color=color, label=label)
plt.legend(loc='upper left')
plt.xlabel('Boosting Iterations')
plt.ylabel('Test Set Deviance')
Text(0, 0.5, 'Test Set Deviance')
# https://plotly.com/python/visualizing-mri-volume-slices/
# https://www.geeksforgeeks.org/animated-data-visualization-using-plotly-express/
import time
import numpy as np
from skimage import io
vol = io.imread("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/attention-mri.tif")
volume = vol.T
r, c = volume[0].shape
# Define frames
import plotly.graph_objects as go
nb_frames = 68
fig = go.Figure(frames=[go.Frame(data=go.Surface(
z=(6.7 - k * 0.1) * np.ones((r, c)),
surfacecolor=np.flipud(volume[67 - k]),
cmin=0, cmax=200
),
name=str(k) # you need to name the frame for the animation to behave properly
)
for k in range(nb_frames)])
# Add data to be displayed before animation starts
fig.add_trace(go.Surface(
z=6.7 * np.ones((r, c)),
surfacecolor=np.flipud(volume[67]),
colorscale='Gray',
cmin=0, cmax=200,
colorbar=dict(thickness=20, ticklen=4)
))
def frame_args(duration):
return {
"frame": {"duration": duration},
"mode": "immediate",
"fromcurrent": True,
"transition": {"duration": duration, "easing": "linear"},
}
sliders = [
{
"pad": {"b": 10, "t": 60},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": [
{
"args": [[f.name], frame_args(0)],
"label": str(k),
"method": "animate",
}
for k, f in enumerate(fig.frames)
],
}
]
# Layout
fig.update_layout(
title='Slices in volumetric data',
width=600,
height=600,
scene=dict(
zaxis=dict(range=[-0.1, 6.8], autorange=False),
aspectratio=dict(x=1, y=1, z=1),
),
updatemenus = [
{
"buttons": [
{
"args": [None, frame_args(50)],
"label": "▶", # play symbol
"method": "animate",
},
{
"args": [[None], frame_args(0)],
"label": "◼", # pause symbol
"method": "animate",
},
],
"direction": "left",
"pad": {"r": 10, "t": 70},
"type": "buttons",
"x": 0.1,
"y": 0,
}
],
sliders=sliders
)
fig.show()